package com.ndphu.comic.gui.list.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ndphu.comic.R;
import com.ndphu.comic.gui.list.model.SlidingMenuItem;

public class SlidingMenuAdatper extends ArrayAdapter<SlidingMenuItem> implements OnClickListener {

	protected int mSelectedIdx = -1;

	protected LayoutInflater mInflater;

	protected Resources mResources;

	protected static int DEFAULT_TEXT_SIZE = 24;

	protected static int SUBITEM_TEXT_SIZE_REDUCED = 6;

	public SlidingMenuAdatper(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mResources = getContext().getResources();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		SlidingMenuItem item = getItem(position);
		if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof ViewHolder)) {
			convertView = mInflater.inflate(R.layout.listitem_navigate_item_selectable, parent, false);
			ViewHolder holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.listitem_navigate_item_selectable_icon);
			holder.textView = (TextView) convertView.findViewById(R.id.listitem_navigate_item_selectable_title);
			holder.expand = (ImageView) convertView.findViewById(R.id.listitem_navigate_item_expand);
			holder.expand.setOnClickListener(this);
			holder.expand.setTag(holder);
			holder.item = item;
			holder.container = (RelativeLayout) convertView.findViewById(R.id.listitem_navigate_item_selectable_container);
			convertView.setTag(holder);
		}

		ViewHolder holder = (ViewHolder) convertView.getTag();
		if (item.expandable == true) {
			holder.expand.setVisibility(View.VISIBLE);
			if (item.isExpanded) {
				holder.expand.setImageResource(R.drawable.ic_listitem_dropdown_collapse);
			} else {
				holder.expand.setImageResource(R.drawable.ic_listitem_dropdown_expand);
			}
		} else {
			holder.expand.setVisibility(View.GONE);
			holder.icon.setVisibility(View.VISIBLE);
		}
		holder.position = position;

		holder.textView.setText(item.title);
		holder.textView.setTextSize(DEFAULT_TEXT_SIZE - item.level * SUBITEM_TEXT_SIZE_REDUCED);
		if (item.icon == null) {
			holder.icon.setVisibility(View.GONE);
		} else {
			holder.icon.setImageBitmap(item.icon);
		}
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(holder.container.getLayoutParams());
		lp.setMargins(36 * item.level, 0, 0, 0);
		holder.container.setLayoutParams(lp);

		if (position == mSelectedIdx) {
			convertView.setBackgroundColor(mResources.getColor(R.color.default_hightligh_color));
			holder.textView.setTextColor(mResources.getColor(android.R.color.white));
		} else {
			convertView.setBackgroundColor(mResources.getColor(android.R.color.white));
			holder.textView.setTextColor(mResources.getColor(android.R.color.black));
		}
		return convertView;
	}

	public void setSelectedIdx(int selectedIdx) {
		this.mSelectedIdx = selectedIdx;
		notifyDataSetChanged();
	}

	public class ViewHolder {
		public int position;
		public ImageView icon;
		public TextView textView;
		public ImageView expand;
		public SlidingMenuItem item;
		public RelativeLayout container;
	}

	@Override
	public int getCount() {
		return super.getCount();
	}

	@Override
	public void onClick(View expandButton) {
		ViewHolder holder = (ViewHolder) expandButton.getTag();
		SlidingMenuItem item = holder.item;
		item.isExpanded = !item.isExpanded;

		List<SlidingMenuItem> tempList = new ArrayList<SlidingMenuItem>();
		int start = holder.position + 1;
		if (item.isExpanded) {
			for (int i = start; i < this.getCount(); ++i) {
				SlidingMenuItem tempItem = this.getItem(i);
				tempList.add(tempItem);
				this.remove(tempItem);
			}
		}

		for (int i = 0; i < item.childs.size(); ++i) {
			SlidingMenuItem child = item.childs.get(i);
			if (item.isExpanded) {
				this.add(child);
			} else {
				this.remove(child);
			}
		}

		for (SlidingMenuItem temp : tempList) {
			this.add(temp);
		}
		notifyDataSetChanged();
	}

}